package com.zero.code;

import org.springframework.util.DigestUtils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class CodeUtils {

    public static String md5Encode(String source){
        return DigestUtils.md5DigestAsHex(source.getBytes());
        /*System.out.println(DigestUtils.md5Digest(source.getBytes()));
        System.out.println(DigestUtils.md5DigestAsHex(source.getBytes()));*/
    }

    public static String sha256Encode(String source) {
        try{
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
            messageDigest.update(source.getBytes());
            return new String(Base64.getEncoder().encode(messageDigest.digest()));
        }catch (NoSuchAlgorithmException e){
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(md5Encode("123456789"));
        System.out.println(sha256Encode("123456789"));
    }
}
